<?php
	/**
	 +---------------------------------------------------------------------------
	 * 调用用户的所有文章信息
	 +---------------------------------------------------------------------------
	 * $user_id:用户的id
	 * $rebuild:是否重建缓存;
	 +---------------------------------------------------------------------------
	*/
	function user_articles($user_id,$rebuild = false){
		if(empty($user_id) or !is_numeric($user_id)) return false;
		if($rebuild){
			$articles_Dao = D('Archives');
			$articles_Vo = $articles_Dao->table('www_archives')->where('mid = '.$user_id)->order('id desc')->findAll();
			if(empty($articles_Vo)) return false;
			S('user_articles_'.$user_id,$articles_Vo);
		}else{
			if(!S('user_articles_'.$user_id)){
				user_articles($user_id,true);
			}
			return S('user_articles_'.$user_id);
		}		
	}
	/**
	 +---------------------------------------------------------------------------
	 * 调用指定ID的文章的信息；
	 +---------------------------------------------------------------------------
	 * $id:文章的ID;
	 * $rebuild：是否重建缓存;
	 +---------------------------------------------------------------------------
	 */
	function article_info($id,$rebuild = false){
	if(empty($id)) return false;
	if($rebuild){
		$dao = D('Arctiny');
		$article_info = $dao->where('id = '.$id)->relation(true)->find();
		//dump($article_info);
		if(empty($article_info)) return false;
		S('article_'.$id,$article_info);
	}else{
		if(!S('article_'.$id)){
			article_info($id,true);
		}
		$article_info = S('article_'.$id);
		return $article_info;
	}
	}
	/**
	+---------------------------------------------------------------------------
	* 设置文章的关键词（栏目）
	+---------------------------------------------------------------------------
	* $keywords:关键词;
	* $id:文章的ID；
	+---------------------------------------------------------------------------
	*/
	function setKeyword($keywords,$aid){
		$keywords = explode(',',$keywords);
		foreach($keywords as $val){
			$taglist[] = insertKeyword($val,$aid);//调用依次插入Tag;
		}
		//cacheKeywords($aid,$taglist);
	}
	
	//插入一个Tag;
	function insertKeyword($keyword,$aid){
		if(!$keyword) exit;
		$keyword = trim($keyword);
		
		$dao = D('Tagindex');
		$data['tag'] = $keyword;
		$vo = $dao->field('id,total')->where("tag like '%".$keyword."%'")->find();
	
		//dump($vo['id'].'->'.$vo['total']);
		if($vo){
			$kw['total'] = $vo['total']+1;
			$tid = $vo['id'];
			$dao->where("id = ".$tid)->save($kw);
		}else{
			$data['total'] = 1;
			$tid = $dao->add($data);
		}	
		if($aid){
			$taglistDao = D('Taglist');
	
			if(!$taglistDao->where("tid=".$tid,"aid=".$aid)->find()){
				$listData['tid'] = $vo['id'];
				$listData['aid'] = $aid;
				$listData['typeid'] = 45;
				$listData['arcrank'] = -1;
				$taglistVo = $taglistDao->add($listData);
			}
		}
		//return '<a href="/tag/'.$tid.'/">'.$keyword.'</a>';
	}
/**
 +--------------------------------------------------------------------
 * 检查验证关键词中是否含有非法的字符;
 +--------------------------------------------------------------------
 */	
	function checkTag($tag){
		$rp = array ("\\","&","'","\"","/","*","<",">","\r","\t","\n","#","$","(",")","%","@","+","?",";","^",":",",","，","  ");
		foreach ($rp as $rparray){
			if (strpos($tag, $rparray) !== FALSE)
			{
				$tag = str_replace($rparray, ' ', $tag);
			}
		}
		return $tag;
	}
/**
 +--------------------------------------------------------------------
 * 检查验证关键词中是否含有非法的字符;
 +--------------------------------------------------------------------
 */	
 	function article_comments($aid,$rebuild = false){
		if(empty($aid)) return false;
		if($rebuild){
			//$commentsDao = D('Feedback');
			//$commentsVo = $commentsDao->where('aid='.$aid)->findall();
			$dao = new Model();
			$commentsVo = $dao->query("select * from www_feedback feed left join www_user_info user on user.uid = feed.mid where aid=".$aid." and user.face <>''");
			if(empty($commentsVo)) return false;
			S('article_comments_'.$aid,$commentsVo);
		}else{
			if(!S('article_comments_'.$aid)){
				article_comments($aid,true);
			}
			return S('article_comments_'.$aid);
		}

	}
/**
 +--------------------------------------------------------------------
 * 栏目信息;
 +--------------------------------------------------------------------
 */	
 	function getCategory($fieldArray,$rebuild = false){
		
		if(is_array($fieldArray)){
		
			 foreach ($fieldArray as $fielditem => $fieldvalue){
			 	$where[$fielditem] = $fieldvalue;
			 }
			 
			 $typeDao = D('Category');
			 $Category_infos = $typeDao->where($where)->find();
			 if(!is_array($Category_infos)) $this->error('你查找的栏目不存在！');
			 return $Category_infos;
			 //dump($Category_infos);
		}
		return false;
	}
?>